package com.ruoyi.project.zerocarbon.controller;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.mapper.SysUserMapper;
import com.ruoyi.project.zerocarbon.domain.*;
import com.ruoyi.project.zerocarbon.domain.export.central.DeclarationCentral1;
import com.ruoyi.project.zerocarbon.domain.export.central.DeclarationCentral2;
import com.ruoyi.project.zerocarbon.domain.export.central.DeclarationCentral3;
import com.ruoyi.project.zerocarbon.domain.export.distribution.DeclarationDistribution1;
import com.ruoyi.project.zerocarbon.domain.export.distribution.DeclarationDistribution2;
import com.ruoyi.project.zerocarbon.domain.export.distribution.DeclarationDistribution3;
import com.ruoyi.project.zerocarbon.domain.export.expert.DeclarationExpert1;
import com.ruoyi.project.zerocarbon.domain.export.expert.DeclarationExpert2;
import com.ruoyi.project.zerocarbon.domain.export.expert.DeclarationExpert3;
import com.ruoyi.project.zerocarbon.domain.export.province.DeclarationProvince1;
import com.ruoyi.project.zerocarbon.domain.export.province.DeclarationProvince2;
import com.ruoyi.project.zerocarbon.domain.export.province.DeclarationProvince3;
import com.ruoyi.project.zerocarbon.domain.export.ranking.DeclarationRanking1;
import com.ruoyi.project.zerocarbon.domain.export.ranking.DeclarationRanking2;
import com.ruoyi.project.zerocarbon.domain.export.ranking.DeclarationRanking3;
import com.ruoyi.project.zerocarbon.domain.vo.DeclarationVo;
import com.ruoyi.project.zerocarbon.mapper.*;
import com.ruoyi.project.zerocarbon.service.IDeclarationService;
import com.ruoyi.project.zerocarbon.service.IDeclareReporterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.ruoyi.common.utils.DateUtils.YYYY_MM_DD_HH_MM_SS;

/**
 * @类名: AuditController
 * @描述:
 * @作者:
 * @日期: 2021/6/26 15:07
 */
@Slf4j
@Api("导出,导出一条时，只需要传id和managementType")
@RequestMapping("/decoration/export")
@RestController
public class DeclarationExportController extends BaseController {

    @Autowired
    private UserRegionMapper userRegionMapper;
    @Autowired
    private DeclarationMapper declarationMapper;
    @Autowired
    private DeclareJudgeMapper declareJudgeMapper;
    @Autowired
    private IDeclarationService declarationService;
    @Autowired
    private IDeclareReporterService declareReporterService;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private DeclareRankingMapper declareRankingMapper;

    @Autowired
    private TokenService tokenService;

    private List<Declaration> selectOne(Long id){
        Declaration declaration = declarationMapper.selectDeclarationById(id);
        List<Declaration> list = Arrays.asList(declaration);
        return list;
    }

    private Declaration selectRankingOne(Long id){
        Declaration declaration = declarationMapper.selectDeclarationById(id);
        List<DeclareJudge> declareJudges = declareJudgeMapper.selectDeclareJudgeByDecorationId(declaration.getId());
        declaration.setDeclareJudges(declareJudges);
        if (!CollectionUtils.isEmpty(declareJudges)){
            Integer sum = declareJudges.stream().mapToInt(DeclareJudge::getScore).sum();
            declaration.setScore(Math.round(sum/5));
        }else {
            declaration.setScore(0);
        }
        DeclareRanking declareRanking = declareRankingMapper.selectDeclareRankingByDeclarationId(declaration.getId());
        Integer score = 0;
        if (declareRanking != null){
            score = declareRanking.getScore();
        }
        int ranking = declarationMapper.getRankingNum(score,declaration.getManagementType()) + 1;
        declaration.setRanking(ranking);

        return declaration;
    }

    private Declaration selectJudgeOne(Long id,String userName,Integer judgeStatus){
        Assert.notNull(userName,"userName cannot be null");
        DeclareJudge declareJudge = new DeclareJudge();
        declareJudge.setDeclarationId(id);
        declareJudge.setExpertNumber(userName);
        if (judgeStatus != null){
            declareJudge.setStatus(judgeStatus);
        }
        List<DeclareJudge> declareJudges = declareJudgeMapper.selectDeclareJudgeList(declareJudge);
        Declaration declaration = declarationMapper.selectDeclarationById(id);

        if (CollectionUtils.isNotEmpty(declareJudges)){
            declaration.setScore(declareJudges.get(0).getScore());
            declaration.setJudgeStatus(declareJudges.get(0).getStatus());
        }else {
            if (judgeStatus != null && judgeStatus != 0){
                return null;
            }
            declaration.setJudgeStatus(0);
        }
        // 推荐星级
        DeclareRanking declareRanking = declareRankingMapper.selectDeclareRankingByDeclarationId(id);
        Integer score = 0;
        if (declareRanking != null){
            score = declareRanking.getScore();
        }
        int ranking = declarationMapper.getRankingNum(score,declaration.getManagementType()) + 1;
        declaration.setRanking(ranking);
        return declaration;
    }

    private List<Declaration> selectList(List<Long> idList){
        List<Declaration> list = new ArrayList<>();
        for (Long id:idList){
            Declaration declaration = declarationMapper.selectDeclarationById(id);
            if (declaration != null){
                list.add(declaration);
            }
        }
        return list;
    }

    /**
     * 项目报名列表
     * @param declarationVo
     * @return
     */
    @ApiOperation("团省委报名列表")
    @PostMapping("/province")
    public AjaxResult provinceList(@Validated @RequestBody DeclarationVo declarationVo) {
        List<Declaration> list = new ArrayList<>();
        if (declarationVo.getId() != null){
            list = selectOne(declarationVo.getId());
        }else if (CollectionUtils.isNotEmpty(declarationVo.getIdList())){
            list = selectList(declarationVo.getIdList());
        }else {
            Integer pageNum = declarationVo.getPageNum()!=null ?declarationVo.getPageNum():1;
            Integer pageSize = declarationVo.getPageSize()!=null ?declarationVo.getPageSize():10000;
            String orderBy = "region_serial desc";
            if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
            {
                PageHelper.startPage(pageNum, pageSize, orderBy);
            }
            declarationVo.setSaveType(1);
            if (declarationVo.getAuditStatus() == null){

            }else if (declarationVo.getAuditStatus() == 13){
                declarationVo.setAuditStatusList(Arrays.asList(13,21,22,23,24,25));
            }
            list = declarationService.getDecorationList(declarationVo);
        }

        if (declarationVo.getManagementType() == 1){
            ExcelUtil<DeclarationProvince1> util = new ExcelUtil<DeclarationProvince1>(DeclarationProvince1.class);
            List<DeclarationProvince1> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                DeclarationProvince1 exportModel = new DeclarationProvince1();
                BeanUtils.copyProperties(declaration,exportModel);
                exportModel.setExportStatus(convertProvinceStatus(declaration.getAuditStatus()));
                if (declaration.getUpdateTime() != null){
                    exportModel.setExportUpdateTime(DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS,declaration.getUpdateTime()));
                }
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "团省委项目");
        }else if(declarationVo.getManagementType() == 2){
            ExcelUtil<DeclarationProvince2> util = new ExcelUtil<DeclarationProvince2>(DeclarationProvince2.class);
            List<DeclarationProvince2> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                DeclarationProvince2 exportModel = new DeclarationProvince2();
                BeanUtils.copyProperties(declaration,exportModel);
                exportModel.setExportStatus(convertProvinceStatus(declaration.getAuditStatus()));
                if (declaration.getUpdateTime() != null){
                    exportModel.setExportUpdateTime(DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS,declaration.getUpdateTime()));
                }
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "团省委学术论著");
        }else {
            ExcelUtil<DeclarationProvince3> util = new ExcelUtil<DeclarationProvince3>(DeclarationProvince3.class);
            List<DeclarationProvince3> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                DeclarationProvince3 exportModel = new DeclarationProvince3();
                BeanUtils.copyProperties(declaration,exportModel);
                exportModel.setExportStatus(convertProvinceStatus(declaration.getAuditStatus()));
                if (declaration.getUpdateTime() != null){
                    exportModel.setExportUpdateTime(DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS,declaration.getUpdateTime()));
                }
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "团省委创意作品");
        }
    }

    private String convertProvinceStatus(Integer status){
//        待审核11、淘汰12、通过13
        if (status == null){
            return null;
        }else if (status == 11){
            return "待审核";
        }else if (status == 12){
            return "淘汰";
        }else if (status == 13){
            return "通过";
        }else if (status > 13){
            return "通过";
        }else {
            return "通过";
        }
    }
    private String convertReportStatus(Integer status){
        //        待提交13、已提交21、已驳回22、
        if (status == null){
            return null;
        }else if (status == 13){
            return "待提交";
        }else if (status == 21){
            return "已提交";
        }else if (status == 22){
            return "已驳回";
        }else if (status > 22){
            return "已提交";
        }else {
            return "已提交";
        }
    }

    /**
     * 团省委上报列表
     * @param declarationVo
     * @return
     */
    @ApiOperation("团省委上报列表")
    @PostMapping("/report")
    public AjaxResult reportList(@Validated @RequestBody DeclarationVo declarationVo) {
        List<Declaration> list = new ArrayList<>();
        if (declarationVo.getId() != null){
            list = selectOne(declarationVo.getId());
        }else if (CollectionUtils.isNotEmpty(declarationVo.getIdList())){
            list = selectList(declarationVo.getIdList());
        }else {
            Integer pageNum = declarationVo.getPageNum()!=null ?declarationVo.getPageNum():1;
            Integer pageSize = declarationVo.getPageSize()!=null ?declarationVo.getPageSize():20;
            String orderBy = "region_serial desc";
            if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
            {
                PageHelper.startPage(pageNum, pageSize, orderBy);
            }
            if (declarationVo.getAuditStatus() == null || declarationVo.getAuditStatus()<13){
                declarationVo.setAuditStatusList(Arrays.asList(13,21,22,23,24,25));
            }
            list = declarationService.getDecorationList(declarationVo);
        }

        if (declarationVo.getManagementType() == 1){
            ExcelUtil<DeclarationProvince1> util = new ExcelUtil<DeclarationProvince1>(DeclarationProvince1.class);
            List<DeclarationProvince1> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                DeclarationProvince1 exportModel = new DeclarationProvince1();
                BeanUtils.copyProperties(declaration,exportModel);
                exportModel.setExportStatus(convertReportStatus(declaration.getAuditStatus()));
                if (declaration.getUpdateTime() != null){
                    exportModel.setExportUpdateTime(DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS,declaration.getUpdateTime()));
                }
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "团省委项目");
        }else if(declarationVo.getManagementType() == 2){
            ExcelUtil<DeclarationProvince2> util = new ExcelUtil<DeclarationProvince2>(DeclarationProvince2.class);
            List<DeclarationProvince2> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                DeclarationProvince2 exportModel = new DeclarationProvince2();
                BeanUtils.copyProperties(declaration,exportModel);
                exportModel.setExportStatus(convertReportStatus(declaration.getAuditStatus()));
                if (declaration.getUpdateTime() != null){
                    exportModel.setExportUpdateTime(DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS,declaration.getUpdateTime()));
                }
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "团省委学术论著");
        }else {
            ExcelUtil<DeclarationProvince3> util = new ExcelUtil<DeclarationProvince3>(DeclarationProvince3.class);
            List<DeclarationProvince3> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                DeclarationProvince3 exportModel = new DeclarationProvince3();
                BeanUtils.copyProperties(declaration,exportModel);
                exportModel.setExportStatus(convertReportStatus(declaration.getAuditStatus()));
                if (declaration.getUpdateTime() != null){
                    exportModel.setExportUpdateTime(DateUtils.parseDateToStr(YYYY_MM_DD_HH_MM_SS,declaration.getUpdateTime()));
                }
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "团省委创意作品");
        }
    }

    /**
     * 项目报名列表
     * @param declarationVo
     * @return
     */
    @ApiOperation("分配列表")
    @PostMapping("/distribution")
    public AjaxResult distributionList(@Validated @RequestBody DeclarationVo declarationVo) {
        List<Declaration> list = new ArrayList<>();
        if (declarationVo.getId() != null){
            list = selectOne(declarationVo.getId());
        }else if (CollectionUtils.isNotEmpty(declarationVo.getIdList())){
                list = selectList(declarationVo.getIdList());
        }else {
            Integer pageNum = declarationVo.getPageNum()!=null ?declarationVo.getPageNum():1;
            Integer pageSize = declarationVo.getPageSize()!=null ?declarationVo.getPageSize():10000;
            String orderBy = "region_serial desc";
            if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
            {
                PageHelper.startPage(pageNum, pageSize, orderBy);
            }

            if (declarationVo.getAuditStatus() == null || declarationVo.getAuditStatus()<23){
                declarationVo.setAuditStatusList(Arrays.asList(24));
            }
            list = declarationService.getDecorationList(declarationVo);
        }
        //0：待提交（草稿）；1：已提交; 2:已报名（APP预约）
        if (declarationVo.getManagementType() == 1){
            ExcelUtil<DeclarationDistribution1> util = new ExcelUtil<DeclarationDistribution1>(DeclarationDistribution1.class);
            List<DeclarationDistribution1> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                DeclarationDistribution1 exportModel = new DeclarationDistribution1();
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                BeanUtils.copyProperties(declaration,exportModel);
                if (declaration.getDistributionStatus() == null || declaration.getDistributionStatus() ==0){
                    exportModel.setExportStatus("待分配");
                }else {
                    exportModel.setExportStatus("已分配");
                }
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "分配列表项目");
        }else if(declarationVo.getManagementType() == 2){
            ExcelUtil<DeclarationDistribution2> util = new ExcelUtil<DeclarationDistribution2>(DeclarationDistribution2.class);
            List<DeclarationDistribution2> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                DeclarationDistribution2 exportModel = new DeclarationDistribution2();
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                BeanUtils.copyProperties(declaration,exportModel);
                if (declaration.getDistributionStatus() == null || declaration.getDistributionStatus() ==0){
                    exportModel.setExportStatus("待分配");
                }else {
                    exportModel.setExportStatus("已分配");
                }
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "分配列表学术论著");
        }else {
            ExcelUtil<DeclarationDistribution3> util = new ExcelUtil<DeclarationDistribution3>(DeclarationDistribution3.class);
            List<DeclarationDistribution3> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                DeclarationDistribution3 exportModel = new DeclarationDistribution3();
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                BeanUtils.copyProperties(declaration,exportModel);
                if (declaration.getDistributionStatus() == null || declaration.getDistributionStatus() ==0){
                    exportModel.setExportStatus("待分配");
                }else {
                    exportModel.setExportStatus("已分配");
                }
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "分配列表创意作品");
        }
    }


    /**
     * 项目报名列表
     * @param declarationVo
     * @return
     */
    @ApiOperation("排名列表")
    @PostMapping("/ranking")
    public AjaxResult rankingList(@Validated @RequestBody DeclarationVo declarationVo) {
        List<Declaration> list = new ArrayList<>();
        if (declarationVo.getId() != null){
            Declaration declaration = selectRankingOne(declarationVo.getId());
            list = Arrays.asList(declaration);
        }else if (CollectionUtils.isNotEmpty(declarationVo.getIdList())){
            for (Long id:declarationVo.getIdList()){
                list.add(selectRankingOne(id));
            }
        }else {
            Integer pageNum = declarationVo.getPageNum()!=null ?declarationVo.getPageNum():1;
            Integer pageSize = declarationVo.getPageSize()!=null ?declarationVo.getPageSize():10000;
            String orderBy = "dr.score desc";
            if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
            {
                PageHelper.startPage(pageNum, pageSize, orderBy);
            }

            if (declarationVo.getAuditStatus() == null || declarationVo.getAuditStatus()<23){
                declarationVo.setAuditStatus(24);
            }
            list = declarationService.getRankingDecorationList(declarationVo);
        }
        if (declarationVo.getManagementType() == 1){
            ExcelUtil<DeclarationRanking1> util = new ExcelUtil<DeclarationRanking1>(DeclarationRanking1.class);
            List<DeclarationRanking1> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                DeclarationRanking1 exportModel = new DeclarationRanking1();
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                BeanUtils.copyProperties(declaration,exportModel);
                if (CollectionUtils.isNotEmpty(declaration.getDeclareJudges())){
                    if (declaration.getDeclareJudges().size() > 0){
                        exportModel.setExpert1(declaration.getDeclareJudges().get(0).getScore());
                    }
                    if (declaration.getDeclareJudges().size() > 1){
                        exportModel.setExpert2(declaration.getDeclareJudges().get(1).getScore());
                    }
                    if (declaration.getDeclareJudges().size() > 2){
                        exportModel.setExpert3(declaration.getDeclareJudges().get(2).getScore());
                    }
                    if (declaration.getDeclareJudges().size() > 3){
                        exportModel.setExpert4(declaration.getDeclareJudges().get(3).getScore());
                    }
                     if (declaration.getDeclareJudges().size() > 4){
                        exportModel.setExpert5(declaration.getDeclareJudges().get(4).getScore());
                    }
                }
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "排名列表项目");
        }else if(declarationVo.getManagementType() == 2){
            ExcelUtil<DeclarationRanking2> util = new ExcelUtil<DeclarationRanking2>(DeclarationRanking2.class);
            List<DeclarationRanking2> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                DeclarationRanking2 exportModel = new DeclarationRanking2();
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                BeanUtils.copyProperties(declaration,exportModel);
                if (CollectionUtils.isNotEmpty(declaration.getDeclareJudges())){
                    if (declaration.getDeclareJudges().size() > 0){
                        exportModel.setExpert1(declaration.getDeclareJudges().get(0).getScore());
                    }
                    if (declaration.getDeclareJudges().size() > 1){
                        exportModel.setExpert2(declaration.getDeclareJudges().get(1).getScore());
                    }
                    if (declaration.getDeclareJudges().size() > 2){
                        exportModel.setExpert3(declaration.getDeclareJudges().get(2).getScore());
                    }
                    if (declaration.getDeclareJudges().size() > 3){
                        exportModel.setExpert4(declaration.getDeclareJudges().get(3).getScore());
                    }
                    if (declaration.getDeclareJudges().size() > 4){
                        exportModel.setExpert5(declaration.getDeclareJudges().get(4).getScore());
                    }
                }
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "排名列表学术论著");
        }else {
            ExcelUtil<DeclarationRanking3> util = new ExcelUtil<DeclarationRanking3>(DeclarationRanking3.class);
            List<DeclarationRanking3> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                DeclarationRanking3 exportModel = new DeclarationRanking3();
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                BeanUtils.copyProperties(declaration,exportModel);
                if (CollectionUtils.isNotEmpty(declaration.getDeclareJudges())){
                    if (declaration.getDeclareJudges().size() > 0){
                        exportModel.setExpert1(declaration.getDeclareJudges().get(0).getScore());
                    }
                    if (declaration.getDeclareJudges().size() > 1){
                        exportModel.setExpert2(declaration.getDeclareJudges().get(1).getScore());
                    }
                    if (declaration.getDeclareJudges().size() > 2){
                        exportModel.setExpert3(declaration.getDeclareJudges().get(2).getScore());
                    }
                    if (declaration.getDeclareJudges().size() > 3){
                        exportModel.setExpert4(declaration.getDeclareJudges().get(3).getScore());
                    }
                    if (declaration.getDeclareJudges().size() > 4){
                        exportModel.setExpert5(declaration.getDeclareJudges().get(4).getScore());
                    }
                }
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "排名列表创意作品");
        }
    }

    /**
     * 评分列表
     * @param declarationVo
     * @return
     */
    @ApiOperation("评分列表")
    @PostMapping("/expert")
    public AjaxResult judgeList(@Validated @RequestBody DeclarationVo declarationVo) {
        List<Declaration> list = new ArrayList<>();
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String groupName = sysUserMapper.selectRemarkByUserId(loginUser.getUser().getUserId());
        if (StringUtils.isEmpty(groupName)){

        }else {
            declarationVo.setGroupName(groupName);
            if (declarationVo.getId() != null){
                Declaration declaration = selectJudgeOne(declarationVo.getId(), loginUser.getUsername(),declarationVo.getJudgeStatus());
                if (declaration != null){
                    list.add(declaration);
                }
            }else if (CollectionUtils.isNotEmpty(declarationVo.getIdList())){
                for (Long id : declarationVo.getIdList()){
                    Declaration declaration = selectJudgeOne(id, loginUser.getUsername(),declarationVo.getJudgeStatus());
                    if (declaration != null){
                        list.add(declaration);
                    }
                }
            }else {
                declarationVo.setDistributionStatus(1);
                if (declarationVo.getAuditStatus() == null || declarationVo.getAuditStatus() < 23) {
                    declarationVo.setAuditStatus(24);
                }
                list = declarationService.getJudgeDecorationList(declarationVo,loginUser.getUser().getUserName());
            }
        }
        if (loginUser.getUser().getUserName().startsWith("XM")){
            declarationVo.setManagementType(1);
        }else if (loginUser.getUser().getUserName().startsWith("XS")){
            declarationVo.setManagementType(2);
        }else {
            declarationVo.setManagementType(3);
        }
        if (declarationVo.getManagementType() == 1){
            ExcelUtil<DeclarationExpert1> util = new ExcelUtil<DeclarationExpert1>(DeclarationExpert1.class);
            List<DeclarationExpert1> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                DeclarationExpert1 exportModel = new DeclarationExpert1();
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                BeanUtils.copyProperties(declaration,exportModel);
                if (declaration.getJudgeStatus() != null){
                    if (declaration.getJudgeStatus() == 0){
                        exportModel.setExportStatus("待评分");
                    }else if (declaration.getJudgeStatus() ==1){
                        exportModel.setExportStatus("已评分");
                    }else if (declaration.getJudgeStatus() ==2){
                        exportModel.setExportStatus("已提交");
                    }
                }
                Integer recNum = 0;
                if (StringUtils.isNotEmpty(declaration.getRecommendNum())){
                    recNum = Integer.valueOf(declaration.getRecommendNum());
                }

                if (recNum > 8 || recNum == 0){
                    exportModel.setStart("☆");
                }else if (recNum >= 4 && recNum<=8){
                    exportModel.setStart("☆☆");
                }else if (recNum <= 3){
                    exportModel.setStart("☆☆☆");
                }
                exportModel.setScore(declaration.getScore());
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "专家评审列表项目");
        }else if(declarationVo.getManagementType() == 2){
            ExcelUtil<DeclarationExpert2> util = new ExcelUtil<DeclarationExpert2>(DeclarationExpert2.class);
            List<DeclarationExpert2> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                DeclarationExpert2 exportModel = new DeclarationExpert2();
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                BeanUtils.copyProperties(declaration,exportModel);
                if (declaration.getJudgeStatus() != null){
                    if (declaration.getJudgeStatus() == 0){
                        exportModel.setExportStatus("待评分");
                    }else if (declaration.getJudgeStatus() ==1){
                        exportModel.setExportStatus("已评分");
                    }else if (declaration.getJudgeStatus() ==2){
                        exportModel.setExportStatus("已提交");
                    }
                }
                Integer recNum = 0;
                if (StringUtils.isNotEmpty(declaration.getRecommendNum())){
                    recNum = Integer.valueOf(declaration.getRecommendNum());
                }

                if (recNum > 8 || recNum == 0){
                    exportModel.setStart("☆");
                }else if (recNum >= 4 && recNum<=8){
                    exportModel.setStart("☆☆");
                }else if (recNum <= 3){
                    exportModel.setStart("☆☆☆");
                }
                exportModel.setScore(declaration.getScore());
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "专家评审列表学术论著");
        }else {
            ExcelUtil<DeclarationExpert3> util = new ExcelUtil<DeclarationExpert3>(DeclarationExpert3.class);
            List<DeclarationExpert3> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                DeclarationExpert3 exportModel = new DeclarationExpert3();
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                BeanUtils.copyProperties(declaration,exportModel);
                if (declaration.getJudgeStatus() != null){
                    if (declaration.getJudgeStatus() == 0){
                        exportModel.setExportStatus("待评分");
                    }else if (declaration.getJudgeStatus() ==1){
                        exportModel.setExportStatus("已评分");
                    }else if (declaration.getJudgeStatus() ==2){
                        exportModel.setExportStatus("已提交");
                    }
                }
                Integer recNum = 0;
                if (StringUtils.isNotEmpty(declaration.getRecommendNum())){
                    recNum = Integer.valueOf(declaration.getRecommendNum());
                }

                if (recNum > 8 || recNum == 0){
                    exportModel.setStart("☆");
                }else if (recNum >= 4 && recNum<=8){
                    exportModel.setStart("☆☆");
                }else if (recNum <= 3){
                    exportModel.setStart("☆☆☆");
                }
                exportModel.setScore(declaration.getScore());
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "专家评审列表创意作品");
        }
    }

    /**
     * decoration
     * @param declarationVo
     * @return
     */
    @ApiOperation("团中央审核列表")
    @PostMapping("/central")
    public AjaxResult checkList(@Validated @RequestBody DeclarationVo declarationVo) {
        List<Declaration> list = new ArrayList<>();
        if (declarationVo.getId() != null){
            list = selectOne(declarationVo.getId());
        }else if (CollectionUtils.isNotEmpty(declarationVo.getIdList())){
            list = selectList(declarationVo.getIdList());
        }else {
            Integer pageNum = declarationVo.getPageNum()!=null ?declarationVo.getPageNum():1;
            Integer pageSize = declarationVo.getPageSize()!=null ?declarationVo.getPageSize():10000;
            String orderBy = "region_serial desc";
            if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize))
            {
                PageHelper.startPage(pageNum, pageSize, orderBy);
            }

            if (declarationVo.getAuditStatus() == null || declarationVo.getAuditStatus()<21){
                declarationVo.setAuditStatusList(Arrays.asList(21,24,25));
            }
            if (StringUtils.isEmpty(declarationVo.getRegion())) {
                declarationVo.setRegion("全国");
            }
            list = declarationService.getDecorationList(declarationVo);
        }
        if (CollectionUtils.isNotEmpty(list)){
            for (Declaration declaration:list){
                DeclareReporter reporter = declareReporterService.selectDeclareReporterByDeclarationId(declaration.getId());
                if (reporter != null){
                    declaration.setReporter(reporter.getReporter());
                    declaration.setReporterContactNumber(reporter.getContactNumber());
                }
            }
        }
        if (declarationVo.getManagementType() == 1){
            ExcelUtil<DeclarationCentral1> util = new ExcelUtil<DeclarationCentral1>(DeclarationCentral1.class);
            List<DeclarationCentral1> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                DeclarationCentral1 exportModel = new DeclarationCentral1();
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                BeanUtils.copyProperties(declaration,exportModel);
                if (declaration.getAuditStatus() == 21){
                    exportModel.setExportStatus("待审核");
                }else if (declaration.getAuditStatus() == 25){
                    exportModel.setExportStatus("已驳回");
                }else {
                    exportModel.setExportStatus("已通过");
                }
                exportModel.setAuditComment(declaration.getAuditComment());
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "团中央审核列表项目");
        }else if(declarationVo.getManagementType() == 2){
            ExcelUtil<DeclarationCentral2> util = new ExcelUtil<DeclarationCentral2>(DeclarationCentral2.class);
            List<DeclarationCentral2> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                DeclarationCentral2 exportModel = new DeclarationCentral2();
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                BeanUtils.copyProperties(declaration,exportModel);
                if (declaration.getAuditStatus() == 21){
                    exportModel.setExportStatus("待审核");
                }else if (declaration.getAuditStatus() == 25){
                    exportModel.setExportStatus("已驳回");
                }else {
                    exportModel.setExportStatus("已通过");
                }
                exportModel.setAuditComment(declaration.getAuditComment());
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "团中央审核列表学术论著");
        }else {
            ExcelUtil<DeclarationCentral3> util = new ExcelUtil<DeclarationCentral3>(DeclarationCentral3.class);
            List<DeclarationCentral3> exportList = new ArrayList<>();
            for (Declaration declaration:list){
                DeclarationCentral3 exportModel = new DeclarationCentral3();
                declaration.setAuthorNumber(declaration.getAuthorNumber()!=null?declaration.getAuthorNumber()+1:1);
                BeanUtils.copyProperties(declaration,exportModel);
                if (declaration.getAuditStatus() == 21){
                    exportModel.setExportStatus("待审核");
                }else if (declaration.getAuditStatus() == 25){
                    exportModel.setExportStatus("已驳回");
                }else {
                    exportModel.setExportStatus("已通过");
                }
                exportModel.setAuditComment(declaration.getAuditComment());
                exportList.add(exportModel);
            }
            return util.exportExcel(exportList, "团中央审核列表创意作品");
        }
    }

    /**
     * 导出【申报】列表
     */
    @GetMapping("/export")
    public AjaxResult export(DeclarationVo declarationVo)
    {
        ExcelUtil<Declaration> util = new ExcelUtil<Declaration>(Declaration.class);
        if (StringUtils.isEmpty(declarationVo.getRegion())){
            Long userId = null;
            if (declarationVo.getUserId() != null){
                userId = declarationVo.getUserId();
            }else {
                LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
                userId = loginUser.getUser().getUserId();
            }
            if (userId != null){
                List<UserRegion> userRegions = userRegionMapper.selectByUserId(userId);
                if (CollectionUtils.isNotEmpty(userRegions)){
                    if (userRegions.get(0).getRegion().equals("全国")){
                        declarationVo.setRegion("全国");
                    }
                }else {
                    return util.exportExcel(new ArrayList<>(), "申报数据");
                }
            }
        }
        List<Declaration> list = declarationService.getDecorationList(declarationVo);
        return util.exportExcel(list, "申报数据");
    }


}
